package com.example.administrator.kotlintabviewpagerfragment

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import kotlinx.android.synthetic.main.activity_main.*
import android.widget.TextView
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.ImageView
import android.widget.LinearLayout
import com.example.administrator.kotlintabviewpagerfragment.adapter.KotlinPagerAdapter
import com.example.administrator.kotlintabviewpagerfragment.fragment.Tab1Fragment
import com.example.administrator.kotlintabviewpagerfragment.fragment.Tab2Fragment
import com.example.administrator.kotlintabviewpagerfragment.fragment.Tab3Fragment
import com.example.administrator.kotlintabviewpagerfragment.fragment.Tab4Fragment
import org.jetbrains.anko.imageResource


class MainActivity : AppCompatActivity(), TabLayout.OnTabSelectedListener {

    override fun onTabReselected(tab: TabLayout.Tab?) {
    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {
    }

    override fun onTabSelected(tab: TabLayout.Tab?) {
        viewpager.currentItem = tab!!.position
    }

    val list = listOf<String>("tab1", "tab2", "tab3","tab4")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.requestWindowFeature(Window.FEATURE_NO_TITLE)
        setContentView(R.layout.activity_main)
        setTab()
        tab.addOnTabSelectedListener(this)
        setItem()
    }


    fun setTab() {
        tab.tabMode = TabLayout.MODE_FIXED
        val tab1: Tab1Fragment = Tab1Fragment()
        val tab2: Tab2Fragment = Tab2Fragment()
        val tab3: Tab3Fragment = Tab3Fragment()
        val tab4: Tab4Fragment = Tab4Fragment()
        var lsit = listOf<Fragment>(tab1, tab2, tab3,tab4)
        viewpager.adapter = KotlinPagerAdapter(lsit, supportFragmentManager)
        tab.setupWithViewPager(viewpager)
    }

    /**
     * 设置tabitem
     * */
    fun setItem() {
        tab.getTabAt(0)?.customView = getTabView(0)
        tab.getTabAt(1)?.customView = getTabView(1)
        tab.getTabAt(2)?.customView = getTabView(2)
        tab.getTabAt(3)?.customView = getTabView(3)
    }


    fun getTabView(position: Int): View {
        val view = LayoutInflater.from(this).inflate(R.layout.tab_item, null)
        val iv_head = view.findViewById(R.id.tab_item_text) as TextView
        val im = view.findViewById(R.id.image) as ImageView
        //我们可以给库函数 arrayOf() 传递每一项的值来创建Array，arrayOf(1, 2, 3) 创建了一个[1, 2, 3] 这样的数组
        when (position) {
            0 -> {
                val mParams = LinearLayout.LayoutParams(66, 66)
                im.layoutParams = mParams
                im.imageResource = R.drawable.tab_item_select
                iv_head.visibility = View.VISIBLE
                iv_head.text = list[position]
            }
            1 -> {
                val mParams = LinearLayout.LayoutParams(66, 66)
                im.layoutParams = mParams
                im.imageResource = R.drawable.tab_item_select2
                iv_head.visibility = View.VISIBLE
                iv_head.text = list[position]
            }
            2-> {
                val mParams = LinearLayout.LayoutParams(66, 66)
                im.layoutParams = mParams
                im.imageResource = R.drawable.tab_item_select3
                iv_head.visibility = View.VISIBLE
                iv_head.text = list[position]
            }
            3 -> {
                val mParams = LinearLayout.LayoutParams(66, 66)
                im.layoutParams = mParams
                im.imageResource = R.drawable.tab_item_select4
                iv_head.visibility = View.VISIBLE
                iv_head.text = list[position]
            }
        }
        return view
    }

}
